﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDimList", "1.0")]
    public class GetDimListAction : BaseAction<GetDimListParams, GetDimListResult>
    {
        protected override ActionResult<GetDimListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDimListParams opParams)
        {
            SER.DimService service = new SER.DimService();
            PagedList<ENT.EDim> serResult = service.GetDimList(opParams.Filter, opParams.Page * 25, 25);
            if(serResult.IsSuccess)
            {
                GetDimListResult result = new GetDimListResult();
                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Total = serResult.Total;
                result.Page = opParams.Page;

                result.Items = new List<GetDimListResult.Item>();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetDimListResult.Item item = new GetDimListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Code = serResult.Data[index].Code;
                    item.Name = serResult.Data[index].Name;
                    result.Items.Add(item);
                }
                return new ActionResult<GetDimListResult>(result);
            }
            else
            {
                return new ActionResult<GetDimListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDimListParams
    {
        public int Page { get; set; }
        public string Filter { get; set; }
    }

    public class GetDimListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();
        public GetDimListResult()
        {

        }

        public class Item
        {
            public Guid Id { get; set; }
            public string Code { get; set; }

            public string Name { get; set; }
        }
    }
}
